#include <Arduino.h>

#include "HCHOsensor.h"
#include <U8g2lib.h>


#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif



#define OLED_RST    5

#define BLUE_LED    6
#define YELLOW_LED  7
#define READ_LED    8

U8G2_SSD1306_128X64_NONAME_1_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/OLED_RST);

void LED_Control(float cValue);

void draw(void) {
    // graphic commands to redraw the complete screen should be placed here  
    u8g2.setFont(u8g_font_unifont   );//u8g_font_osb21 / u8g_font_unifont
    //u8g.setFont(u8g_font_osb21);
    u8g2.drawStr( 12, 21, "[HCHO Device]");
    u8g2.drawHLine(0,23,127);
    u8g2.drawHLine(0,24,127);
    //u8g2.drawFrame(0,0,128,64);
   
  }


void drawBatteryIco(uint16_t x,uint16_t y,uint16_t size,uint8_t b_percent)
{
    //绘制电池图片框架
    uint16_t w_size = size<<1;
    u8g2.drawFrame(x,y,w_size,size);
    u8g2.drawBox(x+w_size,y+(size>>2),w_size>>3,size>>1);
    u8g2.drawBox(x,y,w_size*b_percent/100,size);
}



uint16_t adc_buffer[8];
float getBatteryVoltage()
{
    static uint8_t i=0;
    uint16_t adc_v;
    float Volatage;
    uint16_t sum=0;
    adc_buffer[i++] = analogRead(A0);
    if(i>=8) i = 0;
    for(uint8_t cnt=0;cnt<8;cnt++)
    {
        sum+= adc_buffer[cnt];
    }
    adc_v = sum>>3;
    Volatage = adc_v*5.0/1024;
    return Volatage*2;
}


HCHOSensor m_hsensor(3,2);
void setup() {
   
    pinMode(4,INPUT);
    pinMode(6,OUTPUT);
    pinMode(7,OUTPUT);
    pinMode(8,OUTPUT);
    delay(1000);
    digitalWrite(6,HIGH);
    digitalWrite(7,HIGH);
    digitalWrite(8,HIGH);

    Serial.begin(9600);
    Serial.println("start!");

    u8g2.begin();
    u8g2.enableUTF8Print();

    for(int i=0;i<8;i++)
    {
        getBatteryVoltage();
    }
}

float hocoValue = 0.00;
float f_battery = 0.00;// 
uint8_t bat_prent = 0;
unsigned long bCheckTime;
unsigned long h_prTime;

void loop() {
    // put your main code here, to run repeatedly:
    bool b_SenGetOK;
    

    if(millis()>=bCheckTime)
    {
        bCheckTime+=5000;//相隔5S
        //检测电池电压并计算百分比
        f_battery = getBatteryVoltage();
        if(f_battery >4.1)
        {
            bat_prent = 100;
        }
        else if(f_battery < 3.5)
        {
            bat_prent = 0;
        }
        else
        {
            bat_prent = 100-(4.1-f_battery)*100/(0.6);
        }
    }
    
   
    if(millis()>=h_prTime)
    {
        h_prTime+= 1000;
        b_SenGetOK = m_hsensor.getValue();
        if(b_SenGetOK)
        {
         
            Serial.println("Seneor Get OK!  ");
            Serial.print(m_hsensor.HCHO_Value);
            Serial.println("\tmg/m3");
            LED_Control(m_hsensor.HCHO_Value);
        }
        else
        {
            Serial.println("Seneor Get fail!  \n");
        }
    
    
        if(b_SenGetOK)
        {
            //u8g2.clearDisplay();
            u8g2.firstPage();  
            do {
                draw();
                u8g2.setFont(u8g2_font_logisoso32_tn);//u8g_font_osb21 u8g2_font_logisoso32_tn /u8g2_font_ncenB14_tr
                u8g2.setCursor(10,63); 
                u8g2.print(m_hsensor.HCHO_Value);  //adc_v
                //u8g2.print(f_battery);
                u8g2.setCursor(83,60);
                u8g2.setFont(u8g_font_unifont);
                u8g2.print("mg/m3");
    
                u8g2.setCursor(83,41);
                //u8g2.setFont(u8g_font_unifont);
                u8g2.print(f_battery);
                u8g2.print("V");
                
                drawBatteryIco(88,0,8,bat_prent);
                u8g2.setFont(u8g2_font_6x10_tn);
                u8g2.setCursor(108,7);
                u8g2.print(bat_prent);
    
                //u8g2.sendBuffer();
            } while( u8g2.nextPage() );
        }
        else
        {
            u8g2.firstPage();  
            do {
                draw();
                u8g2.setFont(u8g2_font_ncenB10_te );//u8g_font_osb21 /u8g2_font_ncenB14_tr
                u8g2.setCursor(15,62); 
                u8g2.print("Sensor Error");
            } while( u8g2.nextPage() );
        }
    
    

    }
    
}


void LED_Control(float cValue)
{
    if(cValue>0.09)
    {
        digitalWrite(READ_LED,LOW);
        digitalWrite(YELLOW_LED,HIGH);
        digitalWrite(BLUE_LED,HIGH);
    }
    else if(cValue>0.05)
    {
        digitalWrite(READ_LED,HIGH);
        digitalWrite(YELLOW_LED,LOW);
        digitalWrite(BLUE_LED,HIGH);
    }
    else 
    {
        digitalWrite(READ_LED,HIGH);
        digitalWrite(YELLOW_LED,HIGH);
        digitalWrite(BLUE_LED,LOW);
    }
}

